//function


$(function() {
	bugs.suggest.init();
});


bugs.suggest = {
	BOX_ID : "searchInput",
	TIMEOUT : 500,
	timeoutId : null,
	skip : false,
	eventKeyup : function() {
		var _hsi = bugs.suggest.hsi();
		var si = bugs.suggest.inp();
		//bugs.log.log(_hsi.val() + "/" + si.val());
		if(bugs.utils.trim(_hsi.val()) != bugs.utils.trim(si.val())) {
			_hsi.val(si.val());
			if(!bugs.suggest.skip)
				bugs.suggest.open(si.val());
		} else {
		}
		bugs.suggest.timeoutId = setTimeout(bugs.suggest.eventKeyup, bugs.suggest.TIMEOUT);
	},
	claearEventKeyup : function(event) {
		if(bugs.suggest.timeoutId != null) {
			clearTimeout(bugs.suggest.timeoutId);
			bugs.suggest.timeoutId = null;
		}
	},
	disable : false,
	url : function() {
		return g_urlAjax + "/search/suggest";
	},
	init : function() {
		var input = bugs.suggest.inp();
		input.focusin(function() {
			bugs.suggest.eventKeyup();
		}).bind("focusout, blur", bugs.suggest.claearEventKeyup);
		
		if(input.length > 0) {
			input.bind("keyup", function(event) {
				
				//bugs.log.log(event.keyCode);
				//bugs.log.log(event);
				var key = event.keyCode;
				if(key == 13 || key == 229) {
				} else if(key == 27) { // ESC Key
					bugs.suggest.skipSuggest(bugs.suggest.close);
					//bugs.suggest.eventKeyup();
				} else if(key == 38 || key == 40) {
					bugs.suggest.keyAction(key, input, event);
				} else {
					if(bugs.suggest.timeoutId == null) {
						bugs.suggest.eventKeyup();
					}
					//bugs.suggest.open(input.val());
				}
			});
		}
		
	},
	box : function() {
		if(typeof bugs.suggest._box == "undefined") {
			bugs.suggest._box = $("#suggestSearch");
		}
		return bugs.suggest._box;		
	},
	skipSuggest : function(func) {
		bugs.suggest.skip= true;
		bugs.suggest.hsi(bugs.suggest.inp().val());
		if(typeof func == "function") func(); 
		bugs.suggest.skip= false;
	},
	inp : function() {
		var _isp;
		if(typeof bugs.suggest._inp == "undefined") {
			bugs.suggest._inp = $("#searchInput");
		}
		_isp = bugs.suggest._inp;
		
		if(typeof value == "undefined") {
			return _isp;
		} else {
			return _isp.val(value);
		}
	},
	hsi : function(value) {
		var _hsi;
		if(typeof bugs.suggest._hsi == "undefined") {
			bugs.suggest._hsi = $("#_hsi");
		} 
		_hsi = bugs.suggest._hsi;

		if(typeof value == "undefined") {
			return _hsi;
		} else {
			return _hsi.val(value);
		}
				
	},
	ul : function() {
		return bugs.suggest.box().find("ul");
	},
	li : function() {
		return bugs.suggest.box().find("ul li");
	},
	ulRemove : function() {
		bugs.suggest.ul().remove();
	},
	open : function(query) {
		bugs.suggest._open(query);
	}, 
	_open : function(query) {
		if(bugs.valid.empty(query)) {
			bugs.suggest.close();
			return;
		}
		
		$.ajax({
			url : bugs.suggest.url(),
			data : {"query": query, "decorator" : "blank"},
			type : "GET",	
			dataType : "html",
			timeout : 1000,
			success : function(result) {
				bugs.suggest.ulRemove();
				if(result == null || bugs.utils.trim(result) =="") {
					bugs.suggest.close(); return;
				}
				var oResult;
				try {
					oResult = $(result);
				} catch(e) {
					bugs.suggest.close(); return;
				}
				if(typeof oResult == "undefined" || oResult.find("> li").length == 0) {
					bugs.suggest.close(); return;
				}
				
				var li = oResult.find("li").each(function(idx) {
					$(this).hover(function(event) {
						$(this).addClass("over");
					}, function(event) {
						$(this).removeClass("over");
					});
				});
				bugs.suggest.box().append(oResult);
				bugs.suggest.show();
				
			}, 
			error : function() {
				bugs.suggest.ulRemove();
				bugs.suggest.close();
			}
		});
	},
	close : function() {
		var _hsi = bugs.suggest.hsi();
		var si = bugs.suggest.inp();
		_hsi.val(si.val());
		bugs.suggest.box().hide();
	}, 
	show : function() {
		bugs.suggest.box().show();
		var _func = function(event) {
			if($("#"+bugs.suggest.BOX_ID +", #searchInput").index(event.target) < 0) {
				bugs.suggest.box().hide();
				$(document).unbind("click", _func);
			}
		};
		$(document).click(_func);
		
	}, 
	keyActionKeyword : null,
	keyAction : function(keyCode, input, event) {
		bugs.suggest.claearEventKeyup(event);
		if(bugs.suggest.keyActionKeyword == null)
			bugs.suggest.keyActionKeyword = input.val();
		bugs.suggest._keyAction(keyCode, input, event);
	},
	_keyAction : function(keyCode, input, event) {
		var arrLi = bugs.suggest.box().find("ul li").toArray();
		var idx;

		$(arrLi).each(function(i) {
			if(keyCode == 38) {
				if($(this).hasClass("over")) {
					if(i > 0) {
						idx = i - 1;
					} else if(i == 0) idx = null;
				}
				
			} else if(keyCode == 40) {
				if($(this).hasClass("over")) {
					if(i < arrLi.length - 1) {
						idx = i + 1;
					} else if(i == arrLi.length - 1) idx = arrLi.length - 1;
				}
			}
		});
		
		if((typeof idx == "undefined" || idx == null) && keyCode == 40) {
			idx = 0;
		}
		
		bugs.suggest.li().removeClass("over");
		if(typeof idx == "undefined" || idx == null) {
			if(keyCode == 38) {
				if(bugs.suggest.keyActionKeyword != null) {
					input.val(bugs.suggest.keyActionKeyword);
					bugs.suggest.hsi(bugs.suggest.keyActionKeyword);
					bugs.suggest.keyActionKeyword = null;
				}
				bugs.suggest.eventKeyup();
			}
		} else {
			var selected = $(arrLi[idx]).addClass("over");
			if(selected.parent().hasClass("resultArtist")) {
				input.val(selected.find(".artistInfo strong").text());
			} else {
				input.val(selected.find("a").text());
			}
		}
	}
		
}